package ibmetp.mis.controller.user;

import ibmetp.mis.common.Constants;
import ibmetp.mis.service.PageService;
import ibmetp.mis.serviceImpl.UserServiceImpl;
import ibmetp.mis.vo.Dept;
import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
/**
 * 获取一页的用户列表
 * @author LAN DABIN
  * @create 2011-9-30
 */
public class UserPageServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	     request.setCharacterEncoding(Constants.ENCODING_UTF8);
		
		//当前页号
		int pageNo=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
		System.out.println("pageNo:"+pageNo);
		
		//页面大小
		int pageSize=request.getParameter("rows")!=null?Integer.parseInt(request.getParameter("rows")):2;
		System.out.println("pageSize:"+pageSize);
		
		//查询条件User
		User user=new User();
		user.setUserName(request.getParameter("userName"));
		user.setUserNo(request.getParameter("userNo"));
		
		if(request.getParameter("deptId")!=null){
			Dept dept=new Dept();
			dept.setDeptId(Long.valueOf(request.getParameter("deptId")));
			user.setDept(dept);
			System.out.println(dept.getDeptId());
		}
		
		//System.out.println("SearchConditions:"+user);
		
		//查询数据
		PageService us=new UserServiceImpl();
		Page p=us.getPage(pageNo, pageSize, user);	   
		
		//输出JSON数据
		JSONObject o=new JSONObject();
		o.put("rows",p.getList());
		o.put("total",p.getTotal());
		response.setCharacterEncoding(Constants.ENCODING_UTF8);
		response.getWriter().write(o.toString());
	  
	}
	

	

}
